package com.immediatus.util;


public abstract class Pool<T extends PoolItem> extends GenericPool<T>{

    public Pool(){
        super();
    }

    public Pool(final int size_){
        super(size_);
    }

    public Pool(final int size_, final int growth_){
        super(size_, growth_);
    }

    @Override
    protected T onHandleAllocatePoolItem(){
        final T poolItem = super.onHandleAllocatePoolItem();
        poolItem._Parent = this;
        return poolItem;
    }

    @Override
    protected void onHandleObtainItem(final T item_){
        item_._Recycled = false;
        item_.onObtain();
    }

    @Override
    protected void onHandleRecycleItem(final T item_){
        item_.onRecycle();
        item_._Recycled = true;
    }

    @Override
    public synchronized void recyclePoolItem(final T item_){
        if (item_._Parent == null){
            throw new IllegalArgumentException("Item not assigned to a pool");
        } else if (!item_.isFromPool(this)){
            throw new IllegalArgumentException("Item from another pool");
        } else if (item_.isRecycled()){
            throw new IllegalArgumentException("Item already recycled");
        }

        super.recyclePoolItem(item_);
    }

    public synchronized boolean ownsPoolItem(final T item_){
        return item_._Parent == this;
    }

    void recycle(final PoolItem item_){
        this.recyclePoolItem((T) item_);
    }
}
